//
//  GLScaleAnimation.m
//  LMPhotoVideoSDK
//
//  Created by Aimar on 16/7/20.
//  Copyright © 2016年 魁钺会. All rights reserved.
//

#import "GLScaleAnimation.h"

@interface GLScaleAnimation ()

@property (nonatomic, assign)vec2       from;
@property (nonatomic, assign)vec2       to;
@property (nonatomic, assign)vec2       pivot;

@end

@implementation GLScaleAnimation

- (id)initWithFrom:(vec2)from to:(vec2)to {
    return [self initWithFrom:from to:to pivot:vec2(0, 0)];
}

- (id)initWithFrom:(vec2)from to:(vec2)to pivot:(vec2)pivot {
    self = [super init];
    if (self) {
        self.from = from;
        self.to = to;
        self.pivot = pivot;
    }
    
    return self;
}

- (void)applyTransformation:(float)interpolatedTime outTransform:(Matrix4<float> *)transform {
    float sx = 1.0f;
    float sy = 1.0f;
    
    if (self.from.x != 1.0f || self.to.x != 1.0f) {
        sx = self.from.x + ((self.to.x - self.from.x) * interpolatedTime);
    }
    if (self.from.y != 1.0f || self.to.y != 1.0f) {
        sy = self.from.y + ((self.to.y - self.from.y) * interpolatedTime);
    }
    
    if (self.pivot.x == 0 && self.pivot.y == 0) {
        (*transform) = mat4::Scale(vec2(sx, sy));
    } else {
        Matrix4<float> m;
        m *= mat4::Translate(-self.pivot.x, self.pivot.y, 0);
        m *= mat4::Scale(vec2(sx, sy));
        m *= mat4::Translate(self.pivot.x, -self.pivot.y, 0);
        (*transform) = m * (*transform);
    }
}

@end
